<?php
/**
 *
 * @author 天意
 * @version 1.0
 */

class View{
	private static $_instance = null;
	public $_view = array();
	public $_regsiter = null;
	public function __construct(){
		$this->_regsiter = Regsiter::getInterface();
	}
	public function __call($method,$args){
		if(method_exists($this,'_'.$method))
			return call_user_func_array(array($this,'_'.$method),$args);
		else
			throw new ViewException("视图里没有找到这个方法");
		/*
		 * @see 弃用
		switch(strtolower($method)){
			case 'display':
				$this->_display($args[0]);
			break;
			case 'fetch':
				$this->_fetch($args[0]);
			break;
			case 'assign':
				$this->_assign($args[0],$args[1]);
			break;
		}
		*/
	}
	private function _display($tpl=''){
		$this->_fetch($tpl,true);
	}
	private function _fetch($tpl='',$display=false){
		$regsiter = $this->_regsiter->get('Config');
		$path = dingwork_path.$regsiter['tpl_path'] .'/'.$regsiter['style'].'/'. $tpl;
		if(file_exists($path) && is_file($path)){
			extract($this->getArray(),EXTR_PREFIX_ALL,'view');
			ob_start();
			include_once $path;
			$c = ob_get_contents();
			ob_clean();
			if($display == true){
				echo $c;
			}
			return $c;
		}else{
			throw new ViewException("{$tpl}模板没有被找到");
		}
	}
	private function _assign($var,$value){
		$this->_view[$var] = $value;
	}
	public function getArray(){
		return $this->_view;
	}

}
class ViewException extends DingException{
}